package ModelImplementation

import (
	"salary-payment/Model"
	"time"
)

type CommissionClassification struct {
	itsSalary         float64
	itsCommissionRate float64
	itsReceipts       map[time.Time]SalesReceipt
}

func (cc *CommissionClassification) GetSalary() float64 {
	return cc.itsSalary
}

func (cc *CommissionClassification) GetRate() float64 {
	return cc.itsCommissionRate
}

func (cc *CommissionClassification) GetReceipt(date time.Time) SalesReceipt {
	return cc.itsReceipts[date]
}

func (cc *CommissionClassification) AddSalesReceipt(sr SalesReceipt) {
	cc.itsReceipts[sr.GetSaleDate()] = sr
}

func (cc *CommissionClassification) CalculatePay(pc Model.Paycheck) float64 {
	commission := 0.0

	for date, sr := range cc.itsReceipts {
		dt1 := pc.GetPayPeriodStartDate()
		dt2 := pc.GetPayPeriodEndDate()

		if date.After(dt1) && date.Before(dt2) {
			commission += sr.GetAmount() * cc.itsCommissionRate
		}
	}

	return commission
}

func NewCommissionClassification(salary, rate float64) *CommissionClassification {
	return &CommissionClassification{itsCommissionRate: rate, itsSalary: salary}
}
